#ifndef THINK_THINK_DEBUG_H
#define THINK_THINK_DEBUG_H

static char* think_debug =
	"\n"
	"// +----------------------------------------------------------------------\n"
	"// | ThinkPHP [ WE CAN DO IT JUST THINK ]\n"
	"// +----------------------------------------------------------------------\n"
	"// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.\n"
	"// +----------------------------------------------------------------------\n"
	"// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )\n"
	"// +----------------------------------------------------------------------\n"
	"// | Author: liu21st <liu21st@gmail.com>\n"
	"// +----------------------------------------------------------------------\n"
	"\n"
	"namespace think;\n"
	"\n"
	"use think\\exception\\ClassNotFoundException;\n"
	"use think\\response\\Redirect;\n"
	"\n"
	"class Debug\n"
	"{\n"
	"    /**\n"
	"     * @var array 区间时间信息\n"
	"     */\n"
	"    protected static $info = [];\n"
	"\n"
	"    /**\n"
	"     * @var array 区间内存信息\n"
	"     */\n"
	"    protected static $mem = [];\n"
	"\n"
	"    /**\n"
	"     * 记录时间（微秒）和内存使用情况\n"
	"     * @access public\n"
	"     * @param  string $name  标记位置\n"
	"     * @param  mixed  $value 标记值(留空则取当前 time 表示仅记录时间 否则同时记录时间和内存)\n"
	"     * @return void\n"
	"     */\n"
	"    public static function remark($name, $value = \'\')\n"
	"    {\n"
	"        self::$info[$name] = is_float($value) ? $value : microtime(true);\n"
	"\n"
	"        if (\'time\' != $value) {\n"
	"            self::$mem[\'mem\'][$name]  = is_float($value) ? $value : memory_get_usage();\n"
	"            self::$mem[\'peak\'][$name] = memory_get_peak_usage();\n"
	"        }\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 统计某个区间的时间（微秒）使用情况 返回值以秒为单位\n"
	"     * @access public\n"
	"     * @param  string  $start 开始标签\n"
	"     * @param  string  $end   结束标签\n"
	"     * @param  integer $dec   小数位\n"
	"     * @return string\n"
	"     */\n"
	"    public static function getRangeTime($start, $end, $dec = 6)\n"
	"    {\n"
	"        if (!isset(self::$info[$end])) {\n"
	"            self::$info[$end] = microtime(true);\n"
	"        }\n"
	"\n"
	"        return number_format((self::$info[$end] - self::$info[$start]), $dec);\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 统计从开始到统计时的时间（微秒）使用情况 返回值以秒为单位\n"
	"     * @access public\n"
	"     * @param  integer $dec 小数位\n"
	"     * @return string\n"
	"     */\n"
	"    public static function getUseTime($dec = 6)\n"
	"    {\n"
	"        return number_format((microtime(true) - THINK_START_TIME), $dec);\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 获取当前访问的吞吐率情况\n"
	"     * @access public\n"
	"     * @return string\n"
	"     */\n"
	"    public static function getThroughputRate()\n"
	"    {\n"
	"        return number_format(1 / self::getUseTime(), 2) . \'req/s\';\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 记录区间的内存使用情况\n"
	"     * @access public\n"
	"     * @param  string  $start 开始标签\n"
	"     * @param  string  $end   结束标签\n"
	"     * @param  integer $dec   小数位\n"
	"     * @return string\n"
	"     */\n"
	"    public static function getRangeMem($start, $end, $dec = 2)\n"
	"    {\n"
	"        if (!isset(self::$mem[\'mem\'][$end])) {\n"
	"            self::$mem[\'mem\'][$end] = memory_get_usage();\n"
	"        }\n"
	"\n"
	"        $size = self::$mem[\'mem\'][$end] - self::$mem[\'mem\'][$start];\n"
	"        $a    = [\'B\', \'KB\', \'MB\', \'GB\', \'TB\'];\n"
	"        $pos  = 0;\n"
	"\n"
	"        while ($size >= 1024) {\n"
	"            $size /= 1024;\n"
	"            $pos++;\n"
	"        }\n"
	"\n"
	"        return round($size, $dec) . \" \" . $a[$pos];\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 统计从开始到统计时的内存使用情况\n"
	"     * @access public\n"
	"     * @param  integer $dec 小数位\n"
	"     * @return string\n"
	"     */\n"
	"    public static function getUseMem($dec = 2)\n"
	"    {\n"
	"        $size = memory_get_usage() - THINK_START_MEM;\n"
	"        $a    = [\'B\', \'KB\', \'MB\', \'GB\', \'TB\'];\n"
	"        $pos  = 0;\n"
	"\n"
	"        while ($size >= 1024) {\n"
	"            $size /= 1024;\n"
	"            $pos++;\n"
	"        }\n"
	"\n"
	"        return round($size, $dec) . \" \" . $a[$pos];\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 统计区间的内存峰值情况\n"
	"     * @access public\n"
	"     * @param  string  $start 开始标签\n"
	"     * @param  string  $end   结束标签\n"
	"     * @param  integer $dec   小数位\n"
	"     * @return string\n"
	"     */\n"
	"    public static function getMemPeak($start, $end, $dec = 2)\n"
	"    {\n"
	"        if (!isset(self::$mem[\'peak\'][$end])) {\n"
	"            self::$mem[\'peak\'][$end] = memory_get_peak_usage();\n"
	"        }\n"
	"\n"
	"        $size = self::$mem[\'peak\'][$end] - self::$mem[\'peak\'][$start];\n"
	"        $a    = [\'B\', \'KB\', \'MB\', \'GB\', \'TB\'];\n"
	"        $pos  = 0;\n"
	"\n"
	"        while ($size >= 1024) {\n"
	"            $size /= 1024;\n"
	"            $pos++;\n"
	"        }\n"
	"\n"
	"        return round($size, $dec) . \" \" . $a[$pos];\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 获取文件加载信息\n"
	"     * @access public\n"
	"     * @param  bool $detail 是否显示详细\n"
	"     * @return integer|array\n"
	"     */\n"
	"    public static function getFile($detail = false)\n"
	"    {\n"
	"        $files = get_included_files();\n"
	"\n"
	"        if ($detail) {\n"
	"            $info = [];\n"
	"\n"
	"            foreach ($files as $file) {\n"
	"                $info[] = $file . \' ( \' . number_format(filesize($file) / 1024, 2) . \' KB )\';\n"
	"            }\n"
	"\n"
	"            return $info;\n"
	"        }\n"
	"\n"
	"        return count($files);\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 浏览器友好的变量输出\n"
	"     * @access public\n"
	"     * @param  mixed       $var   变量\n"
	"     * @param  boolean     $echo  是否输出(默认为 true，为 false 则返回输出字符串)\n"
	"     * @param  string|null $label 标签(默认为空)\n"
	"     * @param  integer     $flags htmlspecialchars 的标志\n"
	"     * @return null|string\n"
	"     */\n"
	"    public static function dump($var, $echo = true, $label = null, $flags = ENT_SUBSTITUTE)\n"
	"    {\n"
	"        $label = (null === $label) ? \'\' : rtrim($label) . \':\';\n"
	"\n"
	"        ob_start();\n"
	"        var_dump($var);\n"
	"        $output = preg_replace(\'/\\]\\=\\>\\n(\\s+)/m\', \'] => \', ob_get_clean());\n"
	"\n"
	"        if (IS_CLI) {\n"
	"            $output = PHP_EOL . $label . $output . PHP_EOL;\n"
	"        } else {\n"
	"            if (!extension_loaded(\'xdebug\')) {\n"
	"                $output = htmlspecialchars($output, $flags);\n"
	"            }\n"
	"\n"
	"            $output = \'<pre>\' . $label . $output . \'</pre>\';\n"
	"        }\n"
	"\n"
	"        if ($echo) {\n"
	"            echo($output);\n"
	"            return;\n"
	"        }\n"
	"\n"
	"        return $output;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 调试信息注入到响应中\n"
	"     * @access public\n"
	"     * @param  Response $response 响应实例\n"
	"     * @param  string   $content  返回的字符串\n"
	"     * @return void\n"
	"     */\n"
	"    public static function inject(Response $response, &$content)\n"
	"    {\n"
	"        $config = Config::get(\'trace\');\n"
	"        $type   = isset($config[\'type\']) ? $config[\'type\'] : \'Html\';\n"
	"        $class  = false !== strpos($type, \'\\\\\') ? $type : \'\\\\think\\\\debug\\\\\' . ucwords($type);\n"
	"\n"
	"        unset($config[\'type\']);\n"
	"\n"
	"        if (!class_exists($class)) {\n"
	"            throw new ClassNotFoundException(\'class not exists:\' . $class, $class);\n"
	"        }\n"
	"\n"
	"        /** @var \\think\\debug\\Console|\\think\\debug\\Html $trace */\n"
	"        $trace = new $class($config);\n"
	"\n"
	"        if ($response instanceof Redirect) {\n"
	"            // TODO 记录\n"
	"        } else {\n"
	"            $output = $trace->output($response, Log::getLog());\n"
	"\n"
	"            if (is_string($output)) {\n"
	"                // trace 调试信息注入\n"
	"                $pos = strripos($content, \'</body>\');\n"
	"                if (false !== $pos) {\n"
	"                    $content = substr($content, 0, $pos) . $output . substr($content, $pos);\n"
	"                } else {\n"
	"                    $content = $content . $output;\n"
	"                }\n"
	"            }\n"
	"        }\n"
	"    }\n"
	"}\n"
	"\n"
;
#endif